package mkis.service.root.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HttpUtils {
	
	public static void setCookie(String name,String value,int time,String path,HttpServletResponse response){
		Cookie cookie = new Cookie(name, value);    // 쿠키를 생성한다. 
		
		if(time!=-1){
			cookie.setMaxAge(time);           // 쿠키의 유효기간을  설정한다.
		}
		cookie.setPath(path);               // 쿠키의 유효한 디렉토리를  설정한다.

		response.addCookie(cookie);        // 클라이언트 응답에 쿠키를 추가한다.

	}
	
	public static String getCookie(HttpServletRequest request,String key) {
		Cookie[] cookies=request.getCookies();
		String value=null;
		for(Cookie cookie:cookies) {
			String name=cookie.getName();
			if(key.equals(name)) {
				value=cookie.getValue();
			}
		}
		return value;
	}
	
	public static HttpSession getSession(HttpServletRequest request) {
		return request.getSession();
	}
	
	public static void setSessionValue(HttpServletRequest request,String key,Object value) {
		getSession(request).setAttribute(key, value);
	}
	
	public static Object getSessionValue(HttpServletRequest request,String key) {
		Object obj=getSession(request).getAttribute(key);
		return obj;
	}
	
	public static void reomveSessionValue(HttpServletRequest request,String key) {
		getSession(request).removeAttribute(key);
	}
	public static void invalidateSession(HttpServletRequest request){
		getSession(request).invalidate();
	}
}
